import gradio as gr
import tensorflow as tf
import numpy as np
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# 加载保存的MLP模型
model = tf.keras.models.load_model('best_mlp_model.h5')

# 定义预测函数
def predict(image):
    image = image.reshape(1, -1) / 255.0  # 归一化
    prediction = model.predict(image)
    predicted_digit = np.argmax(prediction[0])  # 获取最大概率对应的数字
    return str(predicted_digit)  # 将数字转换为字符串类型

# 创建Gradio界面
iface = gr.Interface(fn=predict, inputs='sketchpad', outputs='label')

# 启动Gradio界面
iface.launch()